Repository navigation
Conversation
…ed body
The retry-handler branch parses any 200 response body that starts with
"{" to build its RetryHttpResponse. A truncated or otherwise malformed
body raised JSONDecodeError from json.loads before the caller could run
its non-JSON fallback, so sync WebClient calls with the default retry
handlers surfaced a decode error instead of SlackApiError.
Leave parsed_body unset when the parse fails, which restores the
existing fallback in _urllib_api_call.
mwbrooks
left a comment
There was a problem hiding this comment.
Thanks @agustin18, the fix is exactly what #2070 proposed! 🙇
Changing the raised type from JSONDecodeError to SlackApiError can break callers who catch ValueError today, so as-is this would wait for the 4.x release.
@WilliamBergamin do you have an alternative suggestion on how we could merge this without introducing a breaking change?
| try: | ||
| parsed_body = json.loads(body_string) | ||
| resp["parsed_body"] = parsed_body | ||
| except json.decoder.JSONDecodeError: |
There was a problem hiding this comment.
suggestion (non-blocking): Could we guard the json.loads in _request_for_pagination in this PR too? #2070 has this as a 4.x breaking change, so fixing both sync paths together means callers see the raised type change once instead of across two releases.
| def test_non_json_response_body_starting_with_brace_raises_api_error(self): | ||
| # The retry-handler branch parses any 200 body that starts with "{", so a | ||
| # truncated body must not escape from it as a JSONDecodeError. | ||
| response = {"status": 200, "headers": {}, "body": '{"ok": tru'} |
There was a problem hiding this comment.
praise: Thanks for reusing the exact repro from #2070, so this fails on main for the reason the issue describes.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2072 +/- ##
=======================================
Coverage 84.71% 84.72%
=======================================
Files 118 118
Lines 14122 14125 +3
=======================================
+ Hits 11964 11967 +3
Misses 2158 2158 ☔ View full report in Codecov by Harness. |
|
@mwbrooks I looked into this yesterday and figured it would be better to just keep the issue open until the next major release 😅 Reverting back to the previous behavior might be a bug fix more then an actual breaking change, I opened the issue mainly to figure this out before we open a PR for it |
|
An alternative I though of was to create a new error class that extends |
|
Thanks @WilliamBergamin! Agreed, let's skip the dual error class and keep this open for the 4.x release. 🙇 |
_request_for_pagination parsed the body with its own unguarded json.loads, so a malformed body raised JSONDecodeError there as well, even without retry handlers set. Both sync paths now share _parse_response_body, which returns the error payload instead of raising. Pagination then fails through SlackResponse.validate, so callers see SlackApiError in both places.
Summary
Fixes #2070.
A sync
WebClientparses any 200 response body that starts with{inside the retry loop in_perform_urllib_http_request, so the retry handlers receive a parsed body. Thatjson.loadshad no guard: a truncated body raisedJSONDecodeErrorfrom the loop before_urllib_api_callcould run its non-JSON fallback, so callers got a decode error instead ofSlackApiError._request_for_paginationhad its own unguardedjson.loads, and raised the same way even with no retry handlers set.Both sync paths now go through
_parse_response_body, which returns the existing{"ok": False, "error": ...}payload when the body is not JSON._urllib_api_callandSlackResponse.validate()then raiseSlackApiErrorwithReceived a response in a non-JSON format, so callers see the same raised type on both paths.Per the discussion with @mwbrooks and @WilliamBergamin, this is expected to wait for the 4.x release because
JSONDecodeErroris aValueErrorandSlackApiErroris not.Testing
test_non_json_response_body_starting_with_brace_raises_api_error: fails onmainwithjson.decoder.JSONDecodeError: Expecting value: line 1 column 8 (char 7)and passes here.test_non_json_response_body_during_pagination_raises_api_error: same body on the second page; fails onmainwithJSONDecodeErrorand raisesSlackApiErrorhere.python -m pytest tests/-> 1082 passed, 5 skipped, 45 subtests passed. One run oftests/slack_sdk/oauth/state_store/test_sqlalchemy.py::test_expirationfailed once and passed on the next two runs; it is time-based and unrelated to these files.python scripts/codegen.py --path .produces no changes;base_client.pyis not a generated file../scripts/format.sh,./scripts/lint.shand./scripts/run_mypy.shpass, mypy clean on 107 source files.Category
/docs(Documents)/tutorial(PythOnBoardingBot tutorial)tests/integration_tests(Automated tests for this library)Requirements
python3 -m venv .venv && source .venv/bin/activate && ./scripts/run_validation.shafter making the changes.